package com.han.controllers;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.han.bom.Exam;
import com.han.bom.Professor;
import com.han.bom.Question;
import com.han.bom.RealExam;
import com.han.bom.Student;
import com.han.dao.concrete.product.AnswerDAO;
import com.han.dao.concrete.product.ExamDAO;
import com.han.dao.concrete.product.QuestionDAO;
import com.han.dao.concrete.product.UserDAO;

/**
 * Servlet implementation class EditExamServlet
 */
@WebServlet("/Professor/PreEditExamServlet")
public class PreEditExamServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	private static Logger logger = Logger.getLogger(PreEditExamServlet.class.getName());
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PreEditExamServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		logger.info("Entered PreEditExamServlet GET");
		
		Professor professor = null;
		Exam exam = null;
		HttpSession session = request.getSession();
		ExamDAO examdao = new ExamDAO();
		String type = null;
		
		if(session.getAttribute("professor")!=null){
			professor = (Professor) session.getAttribute("professor");
		}
		else{
			
		}
		
		if(request.getParameter("examID")!=null){
			try {
				exam = examdao.retrieveExamById(request.getParameter("examID"));
			} catch (ClassNotFoundException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (NamingException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			}
		}
		else{
			
		}
		
		if(request.getParameter("type")!=null){
			type = request.getParameter("type");
		}
		else{
			
		}
		
		
		if(!((RealExam)exam).isActivated()){
			QuestionDAO questiondao = new QuestionDAO();
			ArrayList<Question> included = new ArrayList<Question>();
			
			try {
				included = questiondao.retrieveQuestionsbyExam(exam.getExamID());
			} catch (ClassNotFoundException e1) {
				logger.error(e1.getMessage(), e1);
				e1.printStackTrace();
			} catch (SQLException e1) {
				logger.error(e1.getMessage(), e1);
				e1.printStackTrace();
			} catch (NamingException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			}		
				
			ArrayList<Question> all = new ArrayList<Question>();
			
			try {
				
				all = questiondao.retrieveAllQuestions();
				
			} catch (ClassNotFoundException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (NamingException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			}
			
			UserDAO userdao = new UserDAO();
			ArrayList<Student> students = null;  

			try {
				professor.setStudents(userdao.retrieveStudentsByProfessor(professor.getUserID()));
				if(type.equals("Real")){
					students = userdao.retrieveStudentsIncludedInExam(exam.getExamID());
					request.setAttribute("studentsTakingExam", students);
				}
			} catch (ClassNotFoundException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (SQLException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			} catch (NamingException e) {
				logger.error(e.getMessage(), e);
				e.printStackTrace();
			}
			
			AnswerDAO answerdao = new AnswerDAO();
			
			for(Question question: all){
				ArrayList<String> choices=null;
				try {
					choices = answerdao.retrieveAnswersByQuestion(question.getQuestionID());
				} catch (ClassNotFoundException e) {
					logger.error(e.getMessage(), e);
					e.printStackTrace();
				} catch (SQLException e) {
					logger.error(e.getMessage(), e);
					e.printStackTrace();
				} catch (NamingException e) {
					logger.error(e.getMessage(), e);
					e.printStackTrace();
				}
				question.setChoices(choices);
				question.setAnswer(choices.get(0));
			}
			
			exam.setQuestions(included);
			request.setAttribute("exam", exam);
			
			for(Question question:exam.getQuestions()){
				ArrayList<String> choices=null;
				try {
					choices = answerdao.retrieveAnswersByQuestion(question.getQuestionID());
				} catch (ClassNotFoundException e) {
					logger.error(e.getMessage(), e);
					e.printStackTrace();
				} catch (SQLException e) {
					logger.error(e.getMessage(), e);
					e.printStackTrace();
				} catch (NamingException e) {
					logger.error(e.getMessage(), e);
					e.printStackTrace();
				}
				question.setChoices(choices);
				question.setAnswer(choices.get(0));
			}
			
			if(exam.isDraft()){
				request.setAttribute("questions",all);
			}
			
			RequestDispatcher rd = null;
			
			if(type.equals("Real")){
				 rd = request.getRequestDispatcher("Exam_Real_Edit.jsp");
			}
			else{
				rd  = request.getRequestDispatcher("Exam_Practice_Edit.jsp");
			}
			
			rd.forward(request, response);
		}
		else{
			
			response.sendRedirect("ActivatedExamsStudentsListServlet?examID="+request.getParameter("examID"));
			
		}
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
